<template>
	<view>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress">
			<view class="icon">
				<image src="../../static/img/addricon.png" mode=""></image>
			</view>
			<view class="right">
				<view class="tel-name">
					<view class="name">
						{{recinfo.name}}
					</view>
					<view class="tel">
						{{recinfo.tel}}
					</view>
				</view>
				<view class="addres">
					{{recinfo.address.region.label}}
					{{recinfo.address.detailed}}
				</view>
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row,index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.img"></image>
					</view>
					<view class="info">
						<view class="title">{{row.name}}</view>
						<view class="spec">选择{{row.spec}} 数量:{{row.number}}</view>
						<view class="price-number">
							<view class="price">￥{{row.price*row.number}}</view>
							<view class="number">
								
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<view class="order">
			<view class="row">
				<view class="left">
					积分 :
				</view>
				<view class="right">
					已扣除 {{int}} 积分抵扣 {{deduction}} 元
				</view>
			</view>
			<view class="row">
				<view class="left">
					备注 :
				</view>
				<view class="right">
					<input placeholder="选填,备注内容" v-model="note" />
				</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsPrice}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					运费
				</view>
				<view class="content">
					￥+{{freight}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					积分抵扣
				</view>
				<view class="content">
					￥-{{deduction}}
				</view>
			</view>
		</view>
		<view class="blck">
			
		</view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">合计:<view class="money">￥{{sumPrice}}</view></view>
				<view class="btn" @tap="toPay">提交订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 封装接口调用
	import order from '../../pagesUser/request/pages/order/confirmation.js';
	
	export default {
		data() {
			return {
				buylist:[],		//订单列表
				goodsPrice:0.0,	//商品合计价格
				sumPrice:0.0,	//用户付款价格
				freight:12.00,	//运费
				note:'',		//备注
				int:0,		//抵扣积分
				deduction:0,	//抵扣价格
				recinfo:{
					id:1,
					name:"大黑哥",
					head:"大",
					tel:"18816881688",
					address:{
						region:{
							label:"广东省-深圳市-福田区",
						},
						detailed:'深南大道1111号无名摩登大厦6楼A2',
					},
					isDefault:true,
				},
				orderSubscribe: [], // 订单信息
			};
		},
		onShow() {
			//加载个人信息
			this.recinfo.name = getApp().globalData.userData.personName;
			this.recinfo.head = getApp().globalData.userData.personUname;
			this.recinfo.tel = getApp().globalData.userData.personTel;
			this.recinfo.address.region.label = getApp().globalData.userData.personAddress;
			this.recinfo.address.detailed = '';
			//页面显示时，加载订单信息
			uni.getStorage({
				key:'buylist',
				success: (ret) => {
					this.buylist = ret.data;
					this.goodsPrice=0;
					//合计
					let len = this.buylist.length;
					for(let i=0;i<len;i++){
						this.goodsPrice = this.goodsPrice + (this.buylist[i].number*this.buylist[i].price);
					}
					// this.deduction = this.int/100;
					// this.sumPrice = this.goodsPrice-this.deduction+this.freight;
					this.sumPrice = this.goodsPrice + this.freight;
					//强制保留两位小数
					this.sumPrice = this.sumPrice.toFixed(2);
					this.goodsPrice = this.goodsPrice.toFixed(2);
					this.freight = this.freight.toFixed(2);
					this.deduction = this.deduction.toFixed(2);
				}
			});
			uni.getStorage({
				key:'selectAddress',
				success: (e) => {
					this.recinfo = e.data;
					uni.removeStorage({
						key:'selectAddress'
					})
				}
			})
		},
		onHide() {
			//页面隐藏清除订单信息
			this.clearOrder();
		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		methods: {
			clearOrder(){
				uni.removeStorage({
					key: 'buylist',
					success: function (res) {
						this.buylist = [];
						console.log('remove buylist success');
					}
				});
			},
			toPay(){
				let self = this;
				console.log(self.buylist);
				let allMoneys = 0;
				let carStrings = '';
				let numStrings = '';
				self.buylist.forEach(item => {
					allMoneys = allMoneys + item.price;
					carStrings = carStrings + item.id + ",";
					numStrings = numStrings + item.number + ",";
				});
				//请求后台，进行商品订单的添加
				const data = {
					//参数
					allMoney: allMoneys,
					carString: carStrings,
					numString: numStrings
				};
				order.addOrderPay(data).then(res => {
					console.log(res.data)
					this.orderSubscribe = res.data;
				});
				
				//商品列表
				let paymentOrder = [];
				let goodsid=[];
				let len = self.buylist.length;
				for(let i=0;i<len;i++){
					paymentOrder.push(self.buylist[i]);
					goodsid.push(self.buylist[i].id);
				}
				if(paymentOrder.length==0){
					uni.showToast({title:'订单信息有误，请重新购买',icon:'none'});
					return ;
				}
				//本地模拟订单提交UI效果
				uni.showLoading({
					title:'正在提交订单...'
				})
				
				// 订单的消息订阅
				//用户发生点击行为或者发起支付回调后，才可以调起订阅消息界面subscriptionMessage(){
				uni.requestSubscribeMessage({
					//需要订阅的消息模板的id的集合,一次调用最多可订阅3条消息
					tmplIds:[ 'jnCFy-TssllQnUadqsxBNPR294lWvYDISKA9-kZ8MvM'],
					success(res) {
						console.log(res)
						self.notice();
						//使用uni.setStorage进行数据的存储
						uni.setStorage({
							key:'paymentOrder',
							data:paymentOrder,
							success: () => {
								uni.hideLoading();
								uni.redirectTo({
									url:"../pay/payment/payment?amount="+self.sumPrice+"&orderId="+self.orderSubscribe.orderId
								})
							}
						})
					}
				});
			},
			// 通知
			notice() {
				let self = this;
				//实际应用中，此处应该提交长连接，模板仅做本地处理。
				var nowDate = new Date();
				var year = nowDate.getFullYear();
				var month = nowDate.getMonth() + 1;
				var day = nowDate.getDate();
				var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
				var minute = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
				month >= 1 && month <= 9 ? (month = "0" + month) : "";
				day >= 0 && day <= 9 ? (day = "0" + day) : "";
				var nowTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
				console.log("当前时间: " + nowTime)
				// 订单编号 {{character_string1.DATA}}
				// 配送方式 {{thing4.DATA}}
				// 收货人名 {{thing8.DATA}}
				// 订单金额 {{amount3.DATA}}
				// 下单时间 {{time2.DATA}}
				const datas = {
					"touser" : getApp().globalData.user.openId,
					"template_id": "jnCFy-TssllQnUadqsxBNPR294lWvYDISKA9-kZ8MvM",
					"data": {
						"character_string1": {
							"value": self.orderSubscribe.orderId
						},
						"thing4": {
							"value": "上门送货，货到付款"
						},
						"thing8": {
							"value": getApp().globalData.userData.personUname
						},
						"amount3": {
							"value": self.orderSubscribe.allMoney
						},
						"time2": {
							"value": nowTime
						}
					}
				};
				uni.request({
					url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + getApp().globalData.user.accessToken,
					data: JSON.stringify(datas),
					method: 'POST',
					success: function(res) {
						console.log(res)
					}
				})
			},
			//选择收货地址
			selectAddress(){
				uni.navigateTo({
					url:'../user/address/address?type=select'
				})
			}
		}
	}
</script>

<style lang="scss">
.addr{
	width: 86%;
	padding: 20upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	display: flex;
	.icon{
		width: 80upx;
		height: 80upx;
		display: flex;
		align-items: center;
		image{
			width: 60upx;
			height: 60upx;
		}
	}
	.tel-name{
		width: 100%;
		display: flex;
		font-size: 32upx;
		.tel{
			margin-left: 40upx;
		}
	}
	.addres{
		width: 100%;
		font-size: 26upx;
		color: #999;
	}
}
.buy-list{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		margin: 30upx 0;
		.goods-info{
			width: 100%;
			display: flex;
			.img{
				width: 22vw;
				height: 22vw;
				border-radius: 10upx;
				overflow: hidden;
				flex-shrink: 0;
				margin-right: 10upx;
				image{
					width: 22vw;
					height: 22vw;
				}
			}
			.info{
				width: 100%;
				height: 22vw;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				position: relative;
				.title{
					width: 100%;
					font-size: 28upx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					// text-align: justify;
					overflow: hidden;
				}
				.spec{
					font-size: 22upx;
					background-color: #f3f3f3;
					color: #a7a7a7;
					height: 40upx;
					display: flex;
					align-items: center;
					padding: 0 10upx;
					border-radius: 20upx;
					margin-bottom: 20vw;
				}
				.price-number{
					position: absolute;
					width: 100%;
					bottom: 0upx;
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					font-size: 28upx;
					height: 40upx;
					.price{
						color: #3E8BC7;
					}
					.number{
						display: flex;
						justify-content: center;
						align-items: center;
						
					}
				}
			}
		}
	}
}
.order{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		margin: 20upx 0;
		height: 40upx;
		display: flex;
		.left{
			font-size: 28upx;
		}
		.right{
			margin-left: 40upx;
			font-size: 26upx;
			color: #999;
			input{
				font-size: 26upx;
				color: #999;
			}
		}
	}
}
.blck{
	width: 100%;
	height: 100upx;
}
.footer{
		width: 92%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		
		.settlement{
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.sum{
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;
				.money{
					font-weight: 600;
				}
			}
			.btn{
				padding: 0 30upx;
				height: 60upx;
				background-color: #3E8BC7;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}
.detail{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		height: 60upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.nominal{
			font-size: 28upx;
		}
		.content{
			font-size: 26upx;
			color: #3E8BC7;
		}
	}
}
</style>
